if (SERVER) then
	AddCSLuaFile("shared.lua")
end

if (CLIENT) then
	SWEP.PrintName = "Wraith Life Sucker"
	SWEP.Slot = 1
	SWEP.SlotPos = 3
	SWEP.DrawAmmo = false
	SWEP.DrawCrosshair = false
end

// Variables that are used on both client and server

SWEP.Category			= "Stargate World Wars"
SWEP.Author 			= "Prefanatic"
SWEP.Instructions 		= "Click, Suck, Live."
SWEP.Contact 			= ""
SWEP.Purpose 			= ""

SWEP.ViewModelFOV = 62
SWEP.ViewModelFlip = false
SWEP.AnimPrefix = "stunstick"

SWEP.Spawnable = false
SWEP.AdminSpawnable = true

SWEP.NextStrike = 0

SWEP.ViewModel = Model("models/weapons/v_stunbaton.mdl")
SWEP.WorldModel = Model("models/weapons/w_stunbaton.mdl")

SWEP.Sound = Sound("weapons/stunstick/stunstick_swing1.wav")

SWEP.Primary.ClipSize = -1      // Size of a clip
SWEP.Primary.DefaultClip = 0        // Default number of bullets in a clip
SWEP.Primary.Automatic = false      // Automatic/Semi Auto
SWEP.Primary.Ammo = ""

SWEP.Secondary.ClipSize = -1        // Size of a clip
SWEP.Secondary.DefaultClip = 0      // Default number of bullets in a clip
SWEP.Secondary.Automatic = false     // Automatic/Semi Auto
SWEP.Secondary.Ammo = ""
sucking = 0
selfNextSuck = 0


/*---------------------------------------------------------
Name: SWEP:Initialize()
Desc: Called when the weapon is first loaded
---------------------------------------------------------*/
function SWEP:Initialize()
	if (SERVER) then
		self:SetWeaponHoldType("melee")
	end
	sucking = 0
	selfNextSuck = 0
	selfNextAttempt = 0
end

/*---------------------------------------------------------
Name: SWEP:Precache()
Desc: Use this function to precache stuff
---------------------------------------------------------*/
function SWEP:Precache()
end

/*---------------------------------------------------------
Name: SWEP:PrimaryAttack()
Desc: +attack1 has been pressed
---------------------------------------------------------*/
function SWEP:PrimaryAttack()
	selfPos = self.Owner:GetPos()
	victim = ents.FindInSphere(selfPos, 100)
	
	for k,v in pairs(victim) do
		if v:IsPlayer() and v:Alive() and v:Nick() != self.Owner:Nick() and CurTime() > selfNextAttempt then
			
			if v:Team() >= 20 and v:Team() < 29 then Notify(self.Owner, "You can't feed on Wraith!", 5) return false end
			
			sucking = 1
			selfNextSuck = 0
			selfNextAttempt = CurTime() + 5
			if v:GetNWBool("ATLANTIS.UPGRADE.IMMUNITYFEED") or v:GetNWBool(v:Nick()..".UPGRADE.IMMUNITYFEED") and SERVER then
				Notify(self.Owner, "You can't feed on a feeding immune person!", 10)
				Notify(v, "You have been protected from the feeding process by your upgrade!", 10)
				return
			end
			
			if CLIENT then
				v:SetDSP(15, false)
			end
			
			if SERVER then
				Notify(self.Owner, "You are currently feeding!  Don't forget, you need to stay in range of your victim!", 10)
				Notify(v, "You are currently being fed upon!", 10)
				GAMEMODE:SetPlayerSpeed(v, 100, 150)
				GAMEMODE:SetPlayerSpeed(self.Owner, 600, 650)
				v:SetNWBool("Feede", true)
				ply:SetNWBool("Feeder", true)
			end
		end
	end
end

function SWEP:SecondaryAttack()
	selfPos = self.Owner:GetPos()
	victim = ents.FindInSphere(selfPos, 100)
	
	for k,v in pairs(victim) do
		if v:IsPlayer() and v:Alive() and v:Nick() != self.Owner:Nick() then
			sucking = 2
			selfNextSuck = 0
			if SERVER then
				Notify(self.Owner, "You are giving some life!", 10)
				Notify(v, "A Wraith is giving you life!", 10)
				GAMEMODE:SetPlayerSpeed(v, 600, 650)
			end
		end
	end
end

function SWEP:Think()
	
	victim = ents.FindInSphere(self.Owner:GetPos(), 100)
	ply = self.Owner
	
	for k,v in pairs(victim) do
		if CurTime() > selfNextSuck and sucking == 1 then
			if v:IsPlayer() and v:Nick() != ply:Nick() then
				selfPos = ply:GetPos()
				selfHP = ply:Health()
				wraithScore = ply:GetNWInt("WRAITHSCORE")
			
				ply:SetHealth(selfHP + 5)
				v:SetHealth(v:Health() - 5)
				selfNextSuck = CurTime() + 1
				if CLIENT then	
					UpdateStatus(ply, v:Health(), selfHP, 1)
				end
				
				if SERVER then
					v:Lock()
					timer.Simple( 2, function() v:UnLock() end)
				end
				
				if v:Health() <= 5 then
					sucking = 0
					if SERVER then
						v:KillSilent()
						ply:SetFrags(ply:Frags() + 1)
						ply:SetNWInt("WRAITHSCORE", wraithScore + 1)
						Notify(ply, "Your pray has been killed.", 5)
					end
				end
			end
		end
		
		if CurTime() > selfNextSuck and sucking == 2 then
			if v:IsPlayer() and v:Nick() != ply:Nick() then
				selfPos = ply:GetPos()
				selfHP = ply:Health()
			
				ply:SetHealth(selfHP - 5)
				v:SetHealth(v:Health() + 5)
				selfNextSuck = CurTime() + 1
				
				if ply:Health() <= 5 then
					sucking = 0
					if SERVER then
						ply:KillSilent()
						Notify(ply, "You have died from lack of life.", 5)
					end
				end
			end
		end
	end
end
